package db

import (
	"github.com/jinzhu/gorm"
)

type IMDepart struct {
	gorm.Model
	Name     string
	ParentId uint
	Status   int
	Organize string
}

var Depart IMDepart

func (d *IMDepart) Add() (departId uint, err error) {
	depart := d.CheckHaveDepartName(d.Name)
	if depart.ID > 0 {
		departId = depart.ID
		err = nil
		return
	}

	err = dbIns.Create(d).Error
	if err != nil {
		return 0, err
	}
	return d.ID, nil
}

func (d *IMDepart) CheckHaveDepartName(name string) (data IMDepart) {
	dbIns.Where("name=?", name).First(&data)
	return
}

func (d *IMDepart) CheckHaveDepartId(id uint) (data IMDepart) {
	dbIns.Where("id=?", id).First(&data)
	return
}

func (d *IMDepart) CheckHaveOrganize(num string) (data IMDepart) {
	dbIns.Where("organize=?", num).First(&data)
	return
}

func (d *IMDepart) Save() {
	dbIns.Save(d)
}

func (d *IMDepart) Delete() {
	dbIns.Delete(d)
}
